package com.example.utils;

import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Map;
import java.io.IOException;

public class YamlUtils{


    // public static Map readYaml(String path) {
    //     Yaml yaml = new Yaml();
    //     Map<String, Object> map = yaml.load(YamlUtils.class.getClassLoader().getResourceAsStream(path));
    //     return map;
    // }

    public static Map<String, Object> readYaml(String path) {
        Yaml yaml = new Yaml();
        try (InputStream inputStream = YamlUtils.class.getClassLoader().getResourceAsStream(path)) {
            if (inputStream == null) {
                throw new FileNotFoundException("Resource not found: " + path);
            }
            return yaml.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
    public static Map readYaml() {
        try {
            Yaml yaml = new Yaml();
            URL url = YamlUtils.class.getClassLoader().getResource("application.yaml");
            if (url != null) {
                
                Object object = yaml.load();
                Map map = yaml.load(new FileInputStream(url.getFile()));
                return map;
            }
            // 泛化的捕获异常
        } catch(FileNotFoundException e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            if (object != null) {
                object.close();
            }
            if (map != null) {
                map.close();
            }
        }
    }*/
}